package com.cryann.mqttserver.config;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;
import io.netty.handler.logging.LoggingHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author cRyann
 * @date 2018/12/4 0004 17:27
 * @description:
 */
@Component
@Slf4j
@ChannelHandler.Sharable
@Data
public class ProtocolInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    NettyEncoder nettyEncoder;

    @Autowired
    NettyServerHandler nettyServerHandler;

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder", new MqttDecoder());
        pipeline.addLast("handler", nettyServerHandler);
        pipeline.addLast("encoder", MqttEncoder.INSTANCE);
        pipeline.addLast("logging", new LoggingHandler());
    }
}
